package com.baseproject.json;

import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.baseproject.util.Const;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by hp on 2017/5/12.
 */

public class JsonUtils implements IJsonProcessor{

    private static JsonUtils intance;
    private IJsonProcessor realJsonProcessor;

    private JsonUtils() {
    }

    public void init(IJsonProcessor jsonProcessor) {
        realJsonProcessor = jsonProcessor;
    }

    public static JsonUtils getInstance() {
        if (null == intance) {
            synchronized (JsonUtils.class) {
                if (null == intance) {
                    intance = new JsonUtils();
                }
            }
        }
        return intance;
    }


    @Override
    public <M> M strToModel(String jsonStr, Class<M> modelClazz) {
        if (!TextUtils.isEmpty(jsonStr)) {
            return realJsonProcessor.strToModel(jsonStr, modelClazz);
        } else {
            return null;
        }
    }

    @Override
    public <M> M strToModel(String jsonStr, Type type) {
        if (!TextUtils.isEmpty(jsonStr)) {
            return realJsonProcessor.strToModel(jsonStr, type);
        } else {
            return null;
        }
    }

    @Override
    public <M> String modelToStr(M model) {
        return realJsonProcessor.modelToStr(model);
    }

    @Override
    public <M> List<M> strToListModel(String jsonStr, Class<M> modelClazz) {
        return realJsonProcessor.strToListModel(jsonStr, modelClazz);
    }

    @Override
    public <M> String listModelToStr(List<M> models) {
        return realJsonProcessor.listModelToStr(models);
    }

    public static JSONObject strToJSONObject(String jsonStr) {
        try {
            return new JSONObject(jsonStr);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    //由于新浪云服务器暂未实名认证会返回多余的无效内容，因此过滤
    public String filterInvaild(String allString) {
        int index = allString.indexOf("<script");
        if(-1 != index) {
             return allString.substring(0, index);
        } else {
            return allString;
        }
    }

    public JSONArray jsonStrToJSONArray(String jsonString) {
        return realJsonProcessor.strToModel(jsonString, JSONArray.class);
    }

    public JSONObject beanToJSONObject(Object bean) {
        JSONObject result = null;
        try {
            result = new JSONObject(realJsonProcessor.modelToStr(bean));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return result;
    }

    public <T> List<T> JSONArrayToList(JSONArray jsonArray, Class<T> clazz) {
        List<T> datas = new ArrayList<>();
        for (int i = 0; i < jsonArray.length(); i++) {
            try {
                datas.add((T) jsonArray.get(i));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return datas;
    }


    @NonNull
    public static JSONObject createJSONObject(JSONArray jsonArrayData) {
        JSONObject jsonObjectData;Map map = new HashMap();
        map.put(Const.JSON_OBJECT_CONTAINER_KEY, jsonArrayData);
        jsonObjectData = new JSONObject(map);
        return jsonObjectData;
    }

    @NonNull
    public static JSONArray createJSONArray(JSONObject jsonObjectData) {
        JSONArray jsonArrayData = new JSONArray();
        jsonArrayData.put(jsonObjectData);
        return jsonArrayData;
    }
}
